<?php
namespace App\Domain;
use App\Model\Video as Model_Video;
use App\Model\Music as Model_Music;
use App\Domain\Vip as Domain_Vip;

class Video {

    /* 我的视频 */
	public function myVideo($uid,$status,$p) {

        $model = new Model_Video();
        $list= $model->myVideo($uid,$status,$p);
        
        $userinfo=\App\getUserInfo($uid);
        
        foreach($list as $k=>$v){
            $v['thumb']=\App\get_upload_path($v['thumb']);
            $v['thumb_s']=\App\get_upload_path($v['thumb_s']);
            $v['href']=\App\get_upload_path($v['href']);
            $v['likes']=\App\NumberFormat($v['likes']);
            $v['views']=\App\NumberFormat($v['views']);
            $v['shares']=\App\NumberFormat($v['shares']);
            $v['comments']=\App\NumberFormat($v['comments']);
            if($v['status']<2){
                $v['reason']='';
            }
            
            $user['id']=$userinfo['id'];
            $user['user_nickname']=$userinfo['user_nickname'];
            $user['avatar']=$userinfo['avatar'];
            $user['avatar_thumb']=$userinfo['avatar_thumb'];
            
            $v['userinfo']=$user;
			
			
			/*判断是否点赞*/
			$islike = $model->islike($uid,$v['id']); 
			$v['islike']=$islike;

            
            $list[$k]=$v;
        }
        
		return $list;
	}

    /* 视频价格 */
	public function getFee() {

        $key='video_fee';
		$list=\App\getcaches($key);
		if(!$list){
            $model = new Model_Video();
			$list= $model->getFee();
            if($list){
                \App\setcaches($key,$list);
            }
			
		}

		return $list;
	}

    /* 发布视频 */
	public function setVideo($data) {
		$rs = array('code' => 0, 'msg' => \PhalApi\T('发布成功'), 'info' => array());
        
//        $uid=$data['uid'];
//        $thumb=$data['thumb'];
//        $href=$data['href'];
        
        /*$isauth=\App\isAuthorAuth($uid);
        if(!$isauth){
            $rs['code']=1003;
			$rs['msg']=\PhalApi\T('您还未认证主播或认证还未通过');
			return $rs;
        }*/
        
        $nowtime=time();
        if($data['isprivate']==1 && $data['coin']<=0){
            $rs['code']=1005;
			$rs['msg']=\PhalApi\T('请选择价格');
			return $rs;
        }
        
        if($data['isprivate']==0){
            $data['coin']='0';
        }
        $data['addtime']=$nowtime;
        $data['uptime']=$nowtime;
        $data['status'] = 1;
		$model = new Model_Video();
        $result = $model->setVideo($data);

        if(!$result){
            $rs['code']=1004;
			$rs['msg']=\PhalApi\T('发布失败，请重试');
			return $rs;
        }
        
        if($data['musicid']){
            /* 更新音乐使用量 */
            $model_m = new Model_Music();
            $result = $model_m->upUses($data['musicid'],1);
        }
        
		return $rs;
	}

    /* 设为公开 */
	public function setPublic($uid,$videoid) {
        
        $data=[
            'isprivate'=>'0',
            'coin'=>'0',
        ];
        
		$model = new Model_Video();
        $result = $model->upVideo($uid,$videoid,$data);

		return $result;
	}

    /* 观看 */
	public function addView($uid,$videoid) {
        
        $rs = array('code' => 0, 'msg' => '', 'info' => array());

		$model = new Model_Video();
        
        $info = $model->getVideo($videoid);
        if(!$info){
            $rs['code']=1003;
			$rs['msg']=\PhalApi\T('视频不存在');
			return $rs;
        }
        
        if($info['uid']==$uid){
            
            $nums=$info['views'];
            $rs['info'][0]['nums']=\App\NumberFormat($nums);
            return $rs;
        }
        
        $model->addView($uid,$videoid);

        $nums=$info['views']+1;
        
        $rs['info'][0]['nums']=\App\NumberFormat($nums);

		return $rs;
	}
    
    /* 点赞 */
	public function addLike($uid,$videoid) {
        
        $rs = array('code' => 0, 'msg' => \PhalApi\T('操作成功'), 'info' => array());

		$model = new Model_Video();
        
        $info = $model->getVideo($videoid);
        if(!$info){
            $rs['code']=1003;
			$rs['msg']=\PhalApi\T('视频不存在');
			return $rs;
        }
        
        /* if($info['uid']==$uid){
            
            $rs['code']=1004;
			$rs['msg']='不能点赞自己的视频';
			return $rs;
        } */
        
        
        $islike = $model->islike($uid,$videoid);
        
        if($islike){
            /* 已点赞 - 取消 */
            $model->reduceLike($uid,$videoid);
            $nums=$info['likes']-1;
        }else{
            /* 未点赞 - 添加 */
            $model->addLike($uid,$videoid);
            $nums=$info['likes']+1;
        }

        
        
        $rs['info'][0]['islike']=$islike?'0':'1';
        
        $rs['info'][0]['nums']=\App\NumberFormat($nums);

		return $rs;
	}
    
    /* 分享 */
	public function addShare($uid,$videoid) {
        
        $rs = array('code' => 0, 'msg' => \PhalApi\T('操作成功'), 'info' => array());
        
		$model = new Model_Video();
        
        $info = $model->getVideo($videoid);
        if(!$info){
            $rs['code']=1003;
			$rs['msg']=\PhalApi\T('视频不存在');
			return $rs;
        }
        
        $model->addShare($uid,$videoid);
        
        $nums=$info['shares']+1;
        
        $rs['info'][0]['nums']=\App\NumberFormat($nums);

		return $rs;
	}

    /* 删除 */
	public function delVideo($uid,$videoid) {
        
        $rs = array('code' => 0, 'msg' => \PhalApi\T('操作成功'), 'info' => array());
        
		$model = new Model_Video();
        
        $where="uid={$uid}";
        
        $info = $model->getVideo($videoid,$where);
        if(!$info){
            $rs['code']=1003;
			$rs['msg']=\PhalApi\T('视频不存在');
			return $rs;
        }
        
        if($info['uid']!=$uid){
            $rs['code']=1004;
			$rs['msg']=\PhalApi\T('操作失败');
			return $rs;
        }
        
        $result = $model->delVideo($videoid);

		return $rs;
	}
    
    /* 举报分类 */
	public function getReport() {

        $key='video_report';
		$list=\App\getcaches($key);
		if(!$list){
            $model = new Model_Video();
			$list= $model->getReport();
            if($list){
                \App\setcaches($key,$list);
            }
		}

		$lang=\PhalApi\DI()->lang;
		foreach($list as $k=>$v){
			if($lang=='en'){
				$v['name']=$v['name_en'];
			}
			$list[$k]=$v;
		}
		
		return $list;
	}

    /* 举报 */
	public function setReport($uid,$videoid,$content) {

        $rs = array('code' => 0, 'msg' => \PhalApi\T('举报成功，平台会马上核实'), 'info' => array());
        
		$model = new Model_Video();
        
        $info = $model->getVideo($videoid);
        if(!$info){
            $rs['code']=1003;
			$rs['msg']=\PhalApi\T('视频不存在');
			return $rs;
        }
        $nowtime=time();
        
        $data=[
            'uid'=>$uid,
            'touid'=>$info['uid'],
            'videoid'=>$videoid,
            'content'=>$content,
            'addtime'=>$nowtime,
            'uptime'=>$nowtime,
        ];
        
        $result = $model->setReport($data);

		return $rs;

	}

    /* 个人视频 */
	public function getHomeVideo($uid,$liveuid,$p) {

        $rs = array('code' => 0, 'msg' => '', 'info' => array());
        
		$model = new Model_Video();
		$domain_vip = new Domain_Vip();
        
        $isvip=0;
        $vipinfo=$domain_vip->myVip($uid);
        if($vipinfo['isvip']){
            $isvip=1;
        }
        
        $where=[];
        $where['uid']=$liveuid;
        
        $userinfo=\App\getUserInfo($liveuid);
        
        $list = $model->getVideoList($where,$p);

        if(!$list){
            $list=[];
        }
        
        if(!empty($list)){
            foreach($list as $k=>$v){
                $v['thumb']=\App\get_upload_path($v['thumb']);
                $v['thumb_s']=\App\get_upload_path($v['thumb_s']);
                $v['href']=\App\get_upload_path($v['href']);
                $v['likes']=\App\NumberFormat($v['likes']);
                $v['views']=\App\NumberFormat($v['views']);
                $v['shares']=\App\NumberFormat($v['shares']);
                $v['comments']=\App\NumberFormat($v['comments']);
                $v['cansee']='0';
                if($v['uid']==$uid){
                    $v['islike']='0';
                    $v['cansee']='1';
                }else{
                    $v['islike']=$model->islike($uid,$v['id']);
                
                    if($v['isprivate']==1){
                        $isbuy=$model->isbuy($uid,$v['id']);
                        if($isbuy || $isvip){
                            $v['cansee']='1';
                        }
                    }else{
                        $v['cansee']='1';
                    }
                }
                
                $user['id']=$userinfo['id'];
                $user['user_nickname']=$userinfo['user_nickname'];
                $user['avatar']=$userinfo['avatar'];
                $user['avatar_thumb']=$userinfo['avatar_thumb'];
                
                $user['online']=$userinfo['online'];
                $user['isvideo']=$userinfo['isvideo'];
                $user['isvoice']=$userinfo['isvoice'];
                $user['video_value']=$userinfo['video_value'];
                $user['voice_value']=$userinfo['voice_value'];
                
                $v['userinfo']=$user;
                
                $list[$k]=$v;
            }
        }
        

        $rs['info']=$list;

		return $rs;

	}

    /* 付费 */
	public function buyVideo($uid,$videoid) {

        $rs = array('code' => 0, 'msg' => \PhalApi\T('操作成功'), 'info' => array());
        
        $model = new Model_Video();
        
        $info = $model->getVideo($videoid);
        if(!$info){
            $rs['code']=1003;
			$rs['msg']=\PhalApi\T('视频不存在');
			return $rs;
        }
        
        if($info['isprivate']==0){
            $rs['code']=1004;
			$rs['msg']=\PhalApi\T('该视频不需要付费');
			return $rs;
        }
        
        $href=\App\get_upload_path($info['href']);
        if($info['uid']==$uid){
            $rs['info'][0]['href']=$href;
            return $rs;
        }
        $total=$info['coin'];
        
        if($total==0){
            $rs['info'][0]['href']=$href;
            return $rs;
        }
        
        $isbuy=$model->isbuy($uid,$info['id']);
        if($isbuy){
            $rs['info'][0]['href']=$href;
            return $rs;
        }
        
        $addtime=time();
		$type='0';
		$action='4';
        
		/* 更新用户余额 消费 */
		$ifok =\App\upCoin($uid,$total);
        if(!$ifok){
            $rs['code'] = 1005;
			$rs['msg'] = \PhalApi\T('余额不足');
			return $rs;
        }

		/* 更新直播 魅力值 累计魅力值 */
		\App\addVotes($info['uid'],$total,$total);

		$insert=array(
            "type"=>$type,
            "action"=>$action,
            "uid"=>$uid,
            "touid"=>$info['uid'],
            "actionid"=>$info['id'],
            "nums"=>1,
            "totalcoin"=>$total,
            "addtime"=>$addtime 
        );
        
        \App\addCoinRecord($insert);

        //增加映票记录
        $data=array(
            'type'=>1,
            'action'=>6, //视频收费
            'uid'=>$info['uid'],
            'fromid'=>$uid,
            'actionid'=>$info['id'],
            'nums'=>1,
            'total'=>$total,
            'showid'=>0,
            'votes'=>$total,
            'addtime'=>$addtime
        );

        \App\addVoteRecord($data);

        $data=[
            'uid'=>$uid,
            'videoid'=>$info['id'],
            'addtime'=>$addtime,
        ];
        $model->buyVideo($data);

        $rs['info'][0]['href']=$href;

		return $rs;

	}

    //获取首页推荐视频列表
    public function getRecommendVideos($uid,$p){

        $model = new Model_Video();
        $rs= $model->getRecommendVideos($uid,$p);
        return $rs;
    }

    public function setComment($data) {
        $rs = array();

        $model = new Model_Video();
        $rs = $model->setComment($data);

        return $rs;
    }

    public function getComments($uid,$videoid,$p) {
        $rs = array();

        $model = new Model_Video();
        $rs = $model->getComments($uid,$videoid,$p);

        return $rs;
    }

    public function getReplys($uid,$commentid,$p) {
        $rs = array();

        $model = new Model_Video();
        $rs = $model->getReplys($uid,$commentid,$p);

        return $rs;
    }

    public function delComments($uid,$videoid,$commentid,$commentuid) {
        $rs = array();

        $model = new Model_Video();
        $rs = $model->delComments($uid,$videoid,$commentid,$commentuid);

        return $rs;
    }

    public function addCommentLike($uid,$commentid) {
        $rs = array();

        $model = new Model_Video();
        $rs = $model->addCommentLike($uid,$commentid);

        return $rs;
    }
	
}
